home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.7 / tcpdump- / tcpdump-richard-1.7 / libpcap-0.0 / pcap-pf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-12  |  8.0 KB  |  314 lines

  1. /*
  2.  * Copyright (c) 1990, 1991, 1992, 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21. #ifndef lint
  22. static  char rcsid[] =
  23.     "@(#)$Header: pcap-pf.c,v 1.32 94/06/10 17:41:01 mccanne Exp $ (LBL)";
  24. #endif
  25.  
  26. /*
  27.  * packet filter subroutines for tcpdump
  28.  *    Extraction/creation by Jeffrey Mogul, DECWRL
  29.  *
  30.  * Extracted from tcpdump.c.
  31.  */
  32.  
  33. #include <sys/types.h>
  34. #include <sys/time.h>
  35. #include <sys/timeb.h>
  36. #include <sys/socket.h>
  37. #include <sys/file.h>
  38. #include <sys/ioctl.h>
  39. #include <net/pfilt.h>
  40.  
  41. #include <net/if.h>
  42. #include <net/bpf.h>
  43.  
  44. #include <netinet/in.h>
  45. #include <netinet/in_systm.h>
  46. #include <netinet/ip.h>
  47. #include <netinet/if_ether.h>
  48. #include <netinet/ip_var.h>
  49. #include <netinet/udp.h>
  50. #include <netinet/udp_var.h>
  51. #include <netinet/tcp.h>
  52. #include <netinet/tcpip.h>
  53.  
  54. #include <ctype.h>
  55. #include <errno.h>
  56. #include <netdb.h>
  57. #include <signal.h>
  58. #include <stdio.h>
  59. #if __STDC__
  60. #include <stdlib.h>
  61. #endif
  62. #include <string.h>
  63. #include <unistd.h>
  64.  
  65. #include "pcap-int.h"
  66.  
  67. /*
  68.  * BUFSPACE is the size in bytes of the packet read buffer.  Most tcpdump
  69.  * applications aren't going to need more than 200 bytes of packet header
  70.  * and the read shouldn't return more packets than packetfilter's internal
  71.  * queue limit (bounded at 256).
  72.  */
  73. #define BUFSPACE (200*256)
  74.  
  75. int
  76. pcap_read_live(pcap_t *pc, int cnt, pcap_handler callback, u_char *user)
  77. {
  78.     u_char *p;
  79.     struct bpf_insn *fcode;
  80.     int cc;
  81.     register u_char *bp;
  82.     int buflen, inc;
  83.     struct enstamp stamp;
  84.     int n;
  85.     fcode = pc->md.use_bpf ? 0 : pc->fcode.bf_insns;
  86.  again:
  87.     cc = pc->cc;
  88.     if (cc == 0) {
  89.         cc = read(pc->fd, (char *)pc->buffer, pc->bufsize);
  90.         if (cc < 0) {
  91.             if (errno == EWOULDBLOCK)
  92.                 return (0);
  93.             if (errno == EINVAL &&
  94.                 (long)(tell(pc->fd) + pc->bufsize) < 0) {
  95.                 /*
  96.                  * Due to a kernel bug, after 2^31 bytes,
  97.                  * the kernel file offset overflows and
  98.                  * read fails with EINVAL. The lseek()
  99.                  * to 0 will fix things.
  100.                  */
  101.                 (void)lseek(pc->fd, 0L, 0);
  102.                 goto again;
  103.             }
  104.             sprintf(pc->errbuf, "pf read: %s",
  105.                 pcap_strerror(errno));
  106.             return (-1);
  107.         }
  108.         bp = pc->buffer;
  109.     } else
  110.         bp = pc->bp;
  111.     /*
  112.      * Loop through each packet.
  113.      */
  114.     n = 0;
  115.     while (cc > 0) {
  116.         /* avoid alignment issues here */
  117.         bcopy((char *)bp, (char *)&stamp, sizeof(stamp));
  118.         if (stamp.ens_stamplen != sizeof(stamp))
  119.             /* buffer is garbage, treat it as poison */
  120.             break;
  121.  
  122.         p = bp + stamp.ens_stamplen;
  123.  
  124.         buflen = stamp.ens_count;
  125.         if (buflen > pc->snapshot)
  126.             buflen = pc->snapshot;
  127.  
  128.         pc->md.TotPkts++;
  129.         pc->md.TotDrops += stamp.ens_dropped;
  130.         pc->md.TotMissed = stamp.ens_ifoverflows;
  131.         if (pc->md.OrigMissed < 0)
  132.             pc->md.OrigMissed = pc->md.TotMissed;
  133.         /*
  134.          * Short-circuit evaluation: if using BPF filter
  135.          * in kernel, no need to do it now.
  136.          */
  137.         if (fcode == 0 ||
  138.             bpf_filter(fcode, p, stamp.ens_count, buflen)) {
  139.             struct pcap_hdr h;
  140.             pc->md.TotAccepted++;
  141.             h.ts = stamp.ens_tstamp;
  142.             h.len = stamp.ens_count;
  143.             h.caplen = buflen;
  144.             h.drops = pc->md.TotDrops;
  145.             (*callback)(user, &h, p);
  146.             if (++n >= cnt && cnt > 0) {
  147.                 inc = ENALIGN(buflen + stamp.ens_stamplen);
  148.                 cc -= inc;
  149.                 bp += inc;
  150.                 pc->cc = cc;
  151.                 pc->bp = bp;
  152.                 return (n);
  153.             }
  154.         }
  155.  
  156.         inc = ENALIGN(buflen + stamp.ens_stamplen);
  157.         cc -= inc;
  158.         bp += inc;
  159.     }
  160.     pc->cc = 0;
  161.     return (n);
  162. }
  163.  
  164. int
  165. pcap_stats_live(pcap_t *p, struct pcap_stat *ps)
  166. {
  167.     ps->ps_recv = p->md.TotPkts;
  168.     ps->ps_drop = p->md.TotDrops;
  169.     ps->ps_ifdrop = p->md.TotMissed - p->md.OrigMissed;
  170.     return (0);
  171. }
  172.  
  173. pcap_t *
  174. pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
  175. {
  176.     pcap_t *p;
  177.     short enmode;
  178.     int backlog = -1;    /* request the most */
  179.     struct enfilter Filter;
  180.     struct endevp devparams;
  181.  
  182.     p = (pcap_t *)malloc(sizeof(*p));
  183.     if (p == 0) {
  184.         strcpy(ebuf, "no swap");
  185.         return (0);
  186.     }
  187.     bzero(p, sizeof(*p));
  188.     p->fd = pfopen(device, 0);
  189.     if (p->fd < 0) {
  190.         sprintf(ebuf, "pf open: %s: %s\n\
  191. your system may not be properly configured; see \"man packetfilter(4)\"\n",
  192.             device, pcap_strerror(errno));
  193.         goto bad;
  194.     }
  195.     p->md.OrigMissed = -1;
  196.     enmode = ENTSTAMP|ENBATCH|ENNONEXCL;
  197.     if (promisc)
  198.         enmode |= ENPROMISC;
  199.     if (ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode) < 0) {
  200.         sprintf(ebuf, "EIOCMBIS: %s", pcap_strerror(errno));
  201.         goto bad;
  202.     }
  203. #ifdef    ENCOPYALL
  204.     /* Try to set COPYALL mode so that we see packets to ourself */
  205.     enmode = ENCOPYALL;
  206.     (void)ioctl(p->fd, EIOCMBIS, (caddr_t)&enmode);/* OK if this fails */
  207. #endif
  208.     /* set the backlog */
  209.     if (ioctl(p->fd, EIOCSETW, (caddr_t)&backlog) < 0) {
  210.         sprintf(ebuf, "EIOCSETW: %s", pcap_strerror(errno));
  211.         goto bad;
  212.     }
  213.     /* set truncation */
  214.     if (ioctl(p->fd, EIOCTRUNCATE, (caddr_t)&snaplen) < 0) {
  215.         sprintf(ebuf, "EIOCTRUNCATE: %s", pcap_strerror(errno));
  216.         goto bad;
  217.     }
  218.     p->snapshot = snaplen;
  219.     /* accept all packets */
  220.     Filter.enf_Priority = 37;    /* anything > 2 */
  221.     Filter.enf_FilterLen = 0;    /* means "always true" */
  222.     if (ioctl(p->fd, EIOCSETF, (caddr_t)&Filter) < 0) {
  223.         sprintf(ebuf, "EIOCSETF: %s", pcap_strerror(errno));
  224.         goto bad;
  225.     }
  226.     /* discover interface type */
  227.     if (ioctl(p->fd, EIOCDEVP, (caddr_t)&devparams) < 0) {
  228.         sprintf(ebuf, "EIOCDEVP: %s", pcap_strerror(errno));
  229.         goto bad;
  230.     }
  231.     /* HACK: to compile prior to Ultrix 4.2 */
  232. #ifndef    ENDT_FDDI
  233. #define    ENDT_FDDI    4
  234. #endif
  235.     switch (devparams.end_dev_type) {
  236.     case ENDT_10MB:
  237.         p->linktype = DLT_EN10MB;
  238.         break;
  239.  
  240.     case ENDT_FDDI:
  241.         p->linktype = DLT_FDDI;
  242.         break;
  243.  
  244.     default:
  245.         /*
  246.          * XXX
  247.          * Currently, the Ultrix packet filter supports only
  248.          * Ethernet and FDDI.  Eventually, support for SLIP and PPP
  249.          * (and possibly others: T1?) should be added.
  250.          */
  251. #ifdef notdef
  252.         warning(
  253.            "Packet filter data-link type %d unknown, assuming Ethernet",
  254.             devparams.end_dev_type);
  255. #endif
  256.         p->linktype = DLT_EN10MB;
  257.         break;
  258.     }
  259.  
  260.     if (to_ms != 0) {
  261.         struct timeval timeout;
  262.         timeout.tv_sec = to_ms / 1000;
  263.         timeout.tv_usec = (to_ms * 1000) % 1000000;
  264.         if (ioctl(p->fd, EIOCSRTIMEOUT, (caddr_t)&timeout) < 0) {
  265.             sprintf(ebuf, "EIOCSRTIMEOUT: %s",
  266.                 pcap_strerror(errno));
  267.             goto bad;
  268.         }
  269.     }
  270.     p->bufsize = BUFSPACE;
  271.     p->buffer = (u_char*)malloc(p->bufsize);
  272.  
  273.     return (p);
  274.  bad:
  275.     free(p);
  276.     return (0);
  277. }
  278.  
  279. int
  280. pcap_setfilter_live(pcap_t *p, struct bpf_program *fp)
  281. {
  282.     /*
  283.      * See if BIOCSETF works.  If it does, the kernel supports
  284.      * BPF-style filters, and we do not need to do post-filtering.
  285.      */
  286.     p->md.use_bpf = (ioctl(p->fd, BIOCSETF, (caddr_t)fp) >= 0);
  287.     if (p->md.use_bpf) {
  288.         struct bpf_version bv;
  289.  
  290.         if (ioctl(p->fd, BIOCVERSION, (caddr_t)&bv) < 0) {
  291.             sprintf(p->errbuf, "BIOCVERSION: %s",
  292.                 pcap_strerror(errno));
  293.             return (-1);
  294.         }
  295.         else if (bv.bv_major != BPF_MAJOR_VERSION ||
  296.              bv.bv_minor < BPF_MINOR_VERSION) {
  297.             fprintf(stderr,
  298.         "requires bpf language %d.%d or higher; kernel is %d.%d",
  299.                 BPF_MAJOR_VERSION, BPF_MINOR_VERSION,
  300.                   bv.bv_major, bv.bv_minor);
  301.             /* don't give up, just be inefficient */
  302.             p->md.use_bpf = 0;
  303.         }
  304.     } else
  305.         p->fcode = *fp;
  306.  
  307.     /*XXX this goes in tcpdump*/
  308.     if (p->md.use_bpf)
  309.         fprintf(stderr, "tcpdump: Using kernel BPF filter\n");
  310.     else
  311.         fprintf(stderr, "tcpdump: Filtering in user process\n");
  312.     return (0);
  313. }
  314.